using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;

public enum EventMgrHandle
{
    VedioPanelStep,
    BgSoundVolum,
}


public class EventMgr : Singleton<EventMgr>
{
    private Dictionary<string, IEventInfo> eventDic = new Dictionary<string, IEventInfo>();

    public void AddEventListener<T>(string name, UnityAction<T> action) {
        // 有没有对应的事件
        // 有
        if(eventDic.ContainsKey(name))
        {
            (eventDic[name] as EventInfo<T>).actions += action;
        }
        // 没有
        else
        {
            eventDic.Add(name, new EventInfo<T>(action));
        }
    }
    public void AddEventListener(string name, UnityAction action)
    {
        // 有没有对应的事件
        // 有
        if(eventDic.ContainsKey(name))
        {
            (eventDic[name] as EventInfo).actions += action;
        }
        // 没有
        else
        {
            eventDic.Add(name, new EventInfo(action));
        }
    }

    // 移除对应的事件监听
    public void RemoveEventListener<T>(string name, UnityAction<T> action)
    {
        if(eventDic.ContainsKey(name))
            (eventDic[name] as EventInfo<T>).actions -= action;
    }
    public void RemoveEventListener(string name, UnityAction action)
    {
        if(eventDic.ContainsKey(name))
            (eventDic[name] as EventInfo).actions -= action;
    }

    // 事件触发
    // 哪一个事件触发了
    public void EventTrigger<T>(string name, T info)
    {
        // 有没有对应的事件
        // 有
        if(eventDic.ContainsKey(name))
        {
            if((eventDic[name] as EventInfo<T>).actions != null)
                (eventDic[name] as EventInfo<T>).actions.Invoke(info);
        }
    }
    // 事件触发，不需要参数
    public void EventTrigger(string name)
    {
        // 有没有对应的事件
        // 有
        if(eventDic.ContainsKey(name))
        {
            if((eventDic[name] as EventInfo).actions != null)
                (eventDic[name] as EventInfo).actions.Invoke();
        }
    }

    // 清空事件
    public void Clear()
    {
        eventDic.Clear();
    }
}

public interface IEventInfo{}

public class EventInfo<T> : IEventInfo {
    public UnityAction<T> actions;
    public EventInfo(UnityAction<T> action) {
        actions += action;
    }
}

public class EventInfo : IEventInfo {
    public UnityAction actions;
    public EventInfo(UnityAction action) {
        actions += action;
    }
}